//
//  mask.swift
//  GOFM
//
//  Created by siri on 2024/1/16.
//  Copyright © 2024 Glad. All rights reserved.
//

import SwiftUI

struct g_popup_bottom<Content: View>: View {
    
    var isOverlayPresented: Binding<Bool>
    var content: () -> Content
    @State private var offset : CGFloat = UIScreen.main.bounds.height
    var body: some View {
        ZStack {
            content()
            if isOverlayPresented.wrappedValue {
                Color.g_color_bg_mask
                    .edgesIgnoringSafeArea(.all)
                    .onTapGesture {
                        withAnimation {
                            offset = UIScreen.main.bounds.height
                            isOverlayPresented.wrappedValue = false
                        }
                    }
                    .offset(y: offset)
                    .onAppear{
                        withAnimation{
                            offset = 0
                        }
                    }
                    .transition(.move(edge: .bottom))
            }
        }
    }
}

struct MaskView: View {
    let maskTitle: String
    @State private var isOverlayPresented = false
    init(maskTitle: String = "", isOverlayPresented: Bool = false) {
        self.maskTitle = maskTitle
        self.isOverlayPresented = isOverlayPresented
    }
    
    var body: some View {
        
        g_popup_bottom(isOverlayPresented: $isOverlayPresented) {
            VStack {
                Button(maskTitle) {
                    withAnimation {
                        isOverlayPresented.toggle()
                    }
                }
                .padding()
            }
        }
    }
}
#if DEBUG
struct MaskView_Previews: PreviewProvider {
    static var previews: some View {
        MaskView(maskTitle:"显示遮罩")
    }
}
#endif
